package com.demo.api;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.jfinal.core.Controller;
import com.jfinal.weixin.sdk.api.AccessTokenApi;
import java.util.Formatter;
import java.util.HashMap;
import java.util.Map;

public class CheckJs  {

	public  static Map<String, String>  setsign(String url){
        Map<String, String> ret = new HashMap<String, String>();
		String noncestr="testhxkj";
		String timestamp=String.valueOf(System.currentTimeMillis()/1000);
		String jsapi_ticket = JsTokenApis.getAccessToken().getAccessToken();
        String string1;
        String signature = "";
        string1 = "jsapi_ticket=" + jsapi_ticket +
                "&noncestr=" + noncestr +
                "&timestamp=" + timestamp +
                "&url=" + url;
        System.out.println("===jsapi_ticket======"+jsapi_ticket);
        System.out.println("===noncestr======"+noncestr);
        System.out.println("===timestamp======"+timestamp);
        System.out.println("===url======"+url);

        try
        {
            MessageDigest crypt = MessageDigest.getInstance("SHA-1");
            crypt.reset();
            crypt.update(string1.getBytes("UTF-8"));
            signature = byteToHex(crypt.digest());
        }
        catch (NoSuchAlgorithmException e)
        {
            e.printStackTrace();
        }
        catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }
        System.out.println("===signature======"+signature);

        ret.put("url", url);
        ret.put("jsapi_ticket", jsapi_ticket);
        ret.put("nonceStr", noncestr);
        ret.put("timestamp", timestamp);
        ret.put("signature", signature);
        return ret;
	}
	public void getJsapi_ticket(){
		JsTokenApis.getAccessToken();
	}
    private static String byteToHex(final byte[] hash) {
        Formatter formatter = new Formatter();
        for (byte b : hash)
        {
            formatter.format("%02x", b);
        }
        String result = formatter.toString();
        formatter.close();
        return result;
    }
public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
	
	Map map= setsign("www.baidu.com");
	System.out.println("=========="+map);

	}
	
}
